package com.hotel.service.impl;

import com.hotel.bean.Room;
import com.hotel.exception.SteduException;
import com.hotel.mapper.RoomMapper;
import com.hotel.mapper.UserMapper;
import com.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private UserMapper userMapper;
    @Caching(
            evict = {
                    @CacheEvict(value = "hotel_system",key = "'roomList'"),
                    @CacheEvict(value = "hotel_system",key = "#room.rid")
            }
    )
    @Override
    public boolean update(Room room) throws SteduException {
        if(roomMapper.selectByRnumber(room.getRnumber()) == null){
            throw new SteduException("房号不存在");
        }
        return roomMapper.update(room) == 1;
    }
    @Caching(
            evict = {
                    @CacheEvict(value = "hotel_system",key = "'roomList'"),
                    @CacheEvict(value = "hotel_system",key = "#rnumber")
            }
    )
    @Override
    public boolean deleteByRnumber(String rnumber) throws SteduException {
        if (roomMapper.selectByRnumber(rnumber) == null) {
            throw new SteduException("房号不存在");
        }
        return roomMapper.deleteByRnumber(rnumber) == 1;
    }
    @Cacheable(value = "hotel_system",key = "#rnumber")
    @Override
    public Room selectByRnumber(String rnumber) {
        return roomMapper.selectByRnumber(rnumber);
    }

    @Override
    public List<Room> selectAll(String rnumber,Integer rsate) {
        return roomMapper.selectAll(rnumber,rsate);
    }
    @Cacheable(value = "hotel_system",key = "#rid")
    @Override
    public List<Integer> selectUidByRid(Integer rid) {
        return roomMapper.selectUidByRid(rid);
    }
    @Cacheable(value = "hotel_system",key = "'roomList'")
    @Override
    public List<Room> selectAllUser() {
        return roomMapper.selectAllUser();
    }

    //退房
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUserUstate(String rnumber) {
        //修改用户状态
        roomMapper.updateUserUstate(rnumber);
        //删除第三张表关联
        roomMapper.deleteRidAndUidByRid(roomMapper.selectByRnumber(rnumber).getRid());
        //房间状态设置为3（打扫中）
        return roomMapper.updateRstate(rnumber,3) == 1;

    }
    //查询所有能预定房间（包括当前订单预定房间）
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Room> selectRoomAndOrderRoom(Integer oid) {
        List<Room> roomList = new ArrayList<>();
        List<Room> rooms1 = roomMapper.selectAllNot4();
        List<Room> rooms2 = roomMapper.selectRoomByOid(oid);
        roomList.addAll(rooms1);
        roomList.addAll(rooms2);
        return roomList;
    }
}
